<template>
  <view>
    <uni-table border stripe emptyText="暂无更多数据">
      <!-- 表头行 -->
      <uni-tr>
        <uni-th align="center" width="170">项目</uni-th>
        <uni-th align="center" width="150" v-for="name in range">{{ name.text }}</uni-th>
      </uni-tr>
      <!-- 表格数据行 -->
      <uni-tr v-for="(item, index) in tableColumn" :key="index">
        <uni-td>{{ item.name }}</uni-td>
        <!-- 对每一个受益人进行遍历，取出每一条数据。 -->
        <uni-td v-for="(name,idx) in range">
          <!-- 在这里需要进行每一类数据的遍历判断，最终显示出实际金额。 -->
          {{ tableData[idx][item.key] }}
        </uni-td>
      </uni-tr>
    </uni-table>
    <view class="bottom" style="padding:20px;display: flex;justify-content: center">
      <button class="button" type="primary" @click="back">返回</button>
      <button class="button" type="primary" @click="edit">返回修改</button>
    </view>
  </view>
</template>

<script>
import userInfoMixin from "../../common/js/userInfoMixin";
import {mapMutations} from 'vuex'

export default {
  mixins: [userInfoMixin],
  data() {
    return {
      sum: 0,
      tableHeader: [
        {
          prop: 'datetime',
          width: 150,
          label: '日期',
          color: '#55C355'
        },
        {
          prop: 'sign_in',
          width: 152,
          label: '上班时间'
        },
        {
          prop: 'sign_out',
          width: 152,
          label: '下班时间'
        },
        {
          prop: 'work_hour',
          width: 110,
          label: '工时'
        },
        {
          prop: 'statusText',
          width: 110,
          label: '状态'
        }
      ],
      name: '300',
      tableData: [],
      value: '',
      range: [{
        text: '张三',
        value: '张三'
      }, {
        text: '李四',
        value: '李四'
      }],
      tableColumn: [
        {
          key: 'dqsl',
          name: '定期申领'
        },
        {
          key: 'rxjlj_bk',
          name: '入学奖励金（本科）'
        },
        {
          key: 'rxjlj_ss',
          name: '入学奖励金（硕士）'
        },
        {
          key: 'rxjlj_bs',
          name: '入学奖励金（博士）'
        },
        {
          key: 'xfzcj_bk',
          name: '学费支持金（本科）'
        },
        {
          key: 'xfzcj_ss',
          name: '学费支持金（硕士）'
        },
        {
          key: 'xfzcj_bs',
          name: '学费支持金（博士）'
        },
        {
          key: 'byjlj_bk',
          name: '毕业奖励金（本科）'
        },
        {
          key: 'byjlj_ss',
          name: '毕业奖励金（硕士）'
        },
        {
          key: 'byjlj_bs',
          name: '毕业奖励金（博士）'
        },
        {
          key: 'jhzfj',
          name: '结婚祝福金'
        },
        {
          key: 'syzfj',
          name: '生育祝福金'
        },
        {
          key: 'hyhxjlj',
          name: '婚姻和谐奖励金'
        },
        {
          key: 'hbshfzj',
          name: '婚变生活扶助金'
        },
        {
          key: 'zyfzzcj',
          name: '职业发展支持金'
        },
        {
          key: 'jntszcj',
          name: '技能提升支持金'
        },
        {
          key: 'cyzcj',
          name: '创业支持金'
        },
        {
          key: 'tbzcj',
          name: '投保支持金'
        },
        {
          key: 'gfxfzcj',
          name: '购房消费支持金'
        },
        {
          key: 'lyzcj',
          name: '旅游支持金'
        },
        {
          key: 'dqshf1',
          name: '定期生活费（伤残）'
        },
        {
          key: 'dqshf2',
          name: '定期生活费（年满60）'
        },
        {
          key: 'zdjbjzj',
          name: '重大疾病救助金'
        },
        {
          key: 'jcsfybj',
          name: '继承税费预备金'
        },
        {
          key: 'fcsybj',
          name: '房产税预备金'
        },
        {
          key: 'jdsl1',
          name: '阶段申领（xx岁）'
        },
        {
          key: 'jdsl2',
          name: '阶段申领（xx岁）'
        },
        {
          key: 'jdsl3',
          name: '阶段申领（xx岁）'
        },
        {
          key: 'jdsl4',
          name: '阶段申领（xx岁）'
        },
        {
          key: 'total',
          name: '合计'
        }
      ],
      jcrTableData: [],
    }
  },
  computed: {},
  methods: {
    ...mapMutations(['setCurrentSyr']),
    back(){
      uni.redirectTo({
        url: '/pages/300fpjqr/300fpjqr',
        success() {
          console.log('succ')
        },
        fail() {
          console.log('failed')
        }
      })
    },
    edit() {
      let that = this
      const db = wx.cloud.database()
      db.collection('JCRS').orderBy('index', 'asc').where({
        openid: that.currentUser.openid
      }).get().then(res1 => {
        that.jcrTableData = res1.data
        console.log('abv: ', res1.data, that.jcrTableData.find(v => v.name === that.value))

        uni.redirectTo({
          url: '/pages/200syrfpj/200syrfpj',
          success: () => {
            console.log('success')
          },
          fail: () => {
            console.log('fail')
          }
        })

      })


    },
    submit() {
      uni.navigateTo({
        url: `/pages/400end/400end`,
        success() {
          console.log('jump succ')
        },
        fail() {
          console.log('jump failed')
        }
      })
    },
    change() {
    },
    getTableData() {
      let db = wx.cloud.database(),
          that = this
      db.collection('JCRS')
          .orderBy('index', 'asc')
          .where({
            openid: that.currentUser.openid,
            name: that.value
          })
          .get()
          .then(res => {
            that.range = res.data.map(v => {
              return {
                value: v.name,
                text: v.name
              }
            })

            console.log('res: ', res)
            that.sum = 0
            that.tableData = res.data.map(v => {

              let item = {
                name: v.name,
                dqsl: v.dqsl && v.dqsl.total || 0,
                rxjlj_bk: (v.rxjlj && v.rxjlj.eduBackground1 ? parseFloat(v.rxjlj.amount1) : 0) || 0,
                rxjlj_ss: (v.rxjlj && v.rxjlj.eduBackground2 ? parseFloat(v.rxjlj.amount2) : 0) || 0,
                rxjlj_bs: (v.rxjlj && v.rxjlj.eduBackground3 ? parseFloat(v.rxjlj.amount3) : 0) || 0,
                xfzcj_bk: (v.xfzcj && v.xfzcj.eduBackground1 ? parseFloat(v.xfzcj.amount1) : 0) || 0,
                xfzcj_ss: (v.xfzcj && v.xfzcj.eduBackground2 ? parseFloat(v.xfzcj.amount2) : 0) || 0,
                xfzcj_bs: (v.xfzcj && v.xfzcj.eduBackground3 ? parseFloat(v.xfzcj.amount3) : 0) || 0,
                byjlj_bk: (v.byjlj && v.byjlj.eduBackground1 ? parseFloat(v.byjlj.amount1) : 0) || 0,
                byjlj_ss: (v.byjlj && v.byjlj.eduBackground2 ? parseFloat(v.byjlj.amount2) : 0) || 0,
                byjlj_bs: (v.byjlj && v.byjlj.eduBackground3 ? parseFloat(v.byjlj.amount3) : 0) || 0,
                jhzfj: v.jhzfj ? parseFloat(v.jhzfj.amount) : 0,
                syzfj: v.syzfj ? parseFloat(v.syzfj.amount) * v.syzfj.times : 0,
                hyhxjlj: v.hyhxjlj ? parseFloat(v.hyhxjlj.amount) : 0,
                hbshfzj: v.hbshfzj ? parseFloat(v.hbshfzj.amount) : 0,
                zyfzzcj: v.zyfzzcj ? parseFloat(v.zyfzzcj.amount) : 0,
                jntszcj: v.jntszcj ? parseFloat(v.jntszcj.amount) * v.jntszcj.times : 0,
                cyzcj: v.cyzcj ? parseFloat(v.cyzcj.amount) : 0,
                tbzcj: v.tbzcj ? parseFloat(v.tbzcj.amount) * v.tbzcj.times : 0,
                gfxfzcj: v.gfxfzcj ? parseFloat(v.gfxfzcj.amount) : 0,
                lyzcj: v.lyzcj ? parseFloat(v.lyzcj.amount) * v.lyzcj.times : 0,
                dqshf1: v.dqshf && v.dqshf.type1.length ? parseFloat(v.dqshf.amount1) * v.dqshf.times1 : 0,
                dqshf2: v.dqshf && v.dqshf.type2.length ? parseFloat(v.dqshf.amount2) * v.dqshf.times2 : 0,
                zdjbjzj: v.zdjbjzj && v.zdjbjzj.type1 ? 10 : 0,// 这个地方固定一下，有需求的话再修改
                jcsfybj: v.jcsfybj && parseFloat(v.jcsfybj.amount) || 0,
                fcsybj: v.fcsybj && parseFloat(v.fcsybj.amount) || 0,
                jdsl1: v.jdsl && v.jdsl.amount1 ? parseFloat(v.jdsl.amount1) : 0,
                jdsl2: v.jdsl && v.jdsl.amount2 ? parseFloat(v.jdsl.amount2) : 0,
                jdsl3: v.jdsl && v.jdsl.amount3 ? parseFloat(v.jdsl.amount3) : 0,
                jdsl4: v.jdsl && v.jdsl.amount4 ? parseFloat(v.jdsl.amount4) : 0
              }
              item.total = Object.keys(item)
                  .filter(v => !['jdsl1', 'jdsl2', 'jdsl3', 'jdsl4', 'name'].includes(v))
                  .reduce((ii, jj) => ii + item[jj], 0)
              that.sum += item.total //最後顯示的總數
              return item
            })

            console.log('确认之后返回：', res)
          })
    }
  },
  created() {
    this.value = this.currentSyr.name
    this.getTableData()
  }
}
</script>

<style>

</style>
